/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
 package com.aries.ui.util;

 import com.aries.ui.widget.BasisDialog;

 import ohos.agp.animation.Animator;
 import ohos.agp.animation.AnimatorProperty;
 import ohos.agp.components.Component;
 import ohos.agp.window.service.DisplayManager;

 import ohos.app.Context;

 /**
  * 动画工具.
  */
 public class AnimationUtils {
     private static AnimationUtils mInstance;
     private Context context;

     /**
      * AnimationUtils.
      *
      * @param context 上下文
      * @return AnimationUtils
      */
     public static AnimationUtils getInstance(Context context) {
         if (mInstance == null) {
             mInstance = new AnimationUtils(context);
         }
         return mInstance;
     }

     private AnimationUtils(Context context) {
         this.context = context;
     }

     /**
      * 显示对话动画.
      *
      * @param contentView Component
      */
     public void showDialogAnimation(Component contentView) {
         AnimatorProperty animOutBOTTOM = createAniminBOTTOM(contentView);
         animOutBOTTOM.setTarget(contentView);
         animOutBOTTOM.setStateChangedListener(new Animator.StateChangedListener() {
             @Override
             public void onStart(Animator animator) {
             }

             @Override
             public void onStop(Animator animator) {
             }

             @Override
             public void onCancel(Animator animator) {
             }

             @Override
             public void onEnd(Animator animator) {
                 animator.release();
             }

             @Override
             public void onPause(Animator animator) {
             }

             @Override
             public void onResume(Animator animator) {
             }
         });
         animOutBOTTOM.start();
     }

     /**
      * 隐藏对话框动画.
      *
      * @param dialog BasisDialog
      */
     public void hideDialogAnimation(BasisDialog dialog) {
         AnimatorProperty animOutBOTTOM = createAnimOutBOTTOM(dialog.getContentView());
         animOutBOTTOM.setTarget(dialog.getContentView());
         animOutBOTTOM.setStateChangedListener(new Animator.StateChangedListener() {
             @Override
             public void onStart(Animator animator) {
             }

             @Override
             public void onStop(Animator animator) {
                 animator.release();
             }

             @Override
             public void onCancel(Animator animator) {
             }

             @Override
             public void onEnd(Animator animator) {
                 dialog.hide();
                 animator.release();
             }

             @Override
             public void onPause(Animator animator) {
             }

             @Override
             public void onResume(Animator animator) {
             }
         });
         animOutBOTTOM.start();
     }


     private AnimatorProperty createAnimOutBOTTOM(Component component) {
         AnimatorProperty animator = new AnimatorProperty();
         int height = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes().height;
         int cokeieBarh = height - component.getHeight();
         animator.moveFromY(cokeieBarh).moveToY(height).alphaFrom(1).alpha(0).setDuration(400);
         return animator;
     }

     private AnimatorProperty createAniminBOTTOM(Component component) {
         AnimatorProperty animator = new AnimatorProperty();
         int height = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes().height;
         int cokeieBarh = height - component.getHeight();
         animator.moveFromY(height).moveToY(cokeieBarh).alphaFrom(0).alpha(1).setDuration(400);
         return animator;
     }
 }
